package org.xfy.sys.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.util.StrUtil;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.xfy.base.util.*;
import org.xfy.org.exception.BusinessException;
import org.xfy.sys.domain.dto.PhoneUserFormDto;
import org.xfy.sys.service.IVerifyCodeService;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private SendMsgWebchinese sendMsgWebchinese;

    @Override
    public AjaxResult getverifyCode(String key) {
        //使用Hutool生成随机图片
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(115, 42, 4, 5);
        //将生成的code放入redis中，key为前端传过来的唯一值
        redisTemplate.opsForValue().set(key,circleCaptcha.getCode(),5, TimeUnit.MINUTES);
        //将生成的图片验证码base64返回给前端
        return AjaxResult.me().setResultObj(circleCaptcha.getImageBase64());
    }

    @Override
    public AjaxResult sendMobileCode(PhoneUserFormDto phoneUserFormDto) {
        //验证redis里面的值不为空
        if(!redisTemplate.hasKey(phoneUserFormDto.getKey())){
            return AjaxResult.me().setSuccess(false).setMessage("验证码已过期");
        }
        //验证图像验证码
        //拿到redis里面的值
        Object o = redisTemplate.opsForValue().get(phoneUserFormDto.getKey());
        if(!phoneUserFormDto.getImageCode().equalsIgnoreCase(o.toString())){
            return AjaxResult.me().setSuccess(false).setMessage("验证码不一致");
        }
        sendMSG(phoneUserFormDto);
        return AjaxResult.me().setMessage("验证成功");
    }

    /**
     * 发送短信
     * @param phoneUserFormDto
     */
    private void sendMSG(PhoneUserFormDto phoneUserFormDto){
        //获取电话号
        String phone = phoneUserFormDto.getPhone();
        //生成随机验证码
        String randomString = StrUtils.getRandomString(6);
        String key=BusinessKey.BK_REGISTER+BusinessKey.BK_SPLIC+phone;
        //防止恶意请求
        String Malicekey=BusinessKey.BK_MALICE+BusinessKey.BK_SPLIC+phone;
        if(redisTemplate.hasKey(Malicekey)){//有Key
            redisTemplate.opsForValue().increment(Malicekey);//加1
            if((Integer) redisTemplate.opsForValue().get(Malicekey)>10){
                //每天的短信次数最多10次
                throw new BusinessException(ErrorCode.Besiness_SYSTEM_Malice_ERROR.getCode());
            }

        }
        redisTemplate.opsForValue().set(Malicekey,1,1,TimeUnit.DAYS);
        //验证里面是否有key
        if(redisTemplate.hasKey(key)){
            //判断时间是否在2分钟以内，如果在2分钟以内返回原来的验证码
            if(redisTemplate.getExpire(key)>60){
                Object o = redisTemplate.opsForValue().get(key);
                randomString=o.toString();
            }
        }
        //发送短信
        sendMsgWebchinese.sendSMS(phone,randomString);
        System.out.println(randomString);
        redisTemplate.opsForValue().set(key,randomString,3,TimeUnit.MINUTES);
    }
}
